package com.sum.tea.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.sum.tea.R

class ReadWriteLockFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.activity_readwritelock, container, false)
        val tb_readwrite = view.findViewById<TabLayout>(R.id.tb_readwrite)
        val viewpage2 = view.findViewById<ViewPager>(R.id.viewpage2)
        getChildFragmentManager().beginTransaction()
        val adapter: FragmentStatePagerAdapter = object : FragmentStatePagerAdapter(
            getChildFragmentManager(),
            BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            private val fragments = arrayOf<Fragment>(
                ReadwriteFragment(),
                LockFragment(),
                DestructionFragment()
            )
            private val titles = arrayOf(
                getString(R.string.tv_reader_write),
                getString(R.string.button_lock),
                getString(R.string.button_kill)
            )

            override fun getItem(position: Int): Fragment {
                return fragments[position]
            }

            override fun getCount(): Int {
                return fragments.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return titles[position]
            }
        }
        viewpage2.adapter = adapter
        tb_readwrite.setupWithViewPager(viewpage2)
        return view
    }
}
